import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;

public class  Controller{
   // Gra asd = new Gra();
    //JLabel label;
    Start start;
    Gra gra;
    int nrGr;
    int kasa;
    
    JButton[] buttons;
    JCheckBox[] chckBoxArray;
    JLabel Komunikaty;
    JLabel nrGracza;
    JLabel kasaStol;
    JLabel[][] Karty;
    JLabel[] kasaGraczy;
    JLabel[] scoreGraczy;
    JFormattedTextField zaklad;
    Card[] cards;
    
    Controller (Start start){
        this.start = start;
    }

    void createGame(/*Start start,*/int iloscGraczy,int iloscBotow, int iloscKasy, int minZaklad) {
        String info;
        
        info = start.createGame(iloscGraczy, iloscBotow, iloscKasy, minZaklad);
        
        showInfo(info);
    }
    
    void startGame(){
        start.start();
    }
    
    void showInfo(String info){
        Komunikaty = gra.getKomunikaty();
        Komunikaty.setText(info);
    }
    
    void showCards(Card[][] karty,int numerGracza){
        //karty[numerGracza][1-5].card[0]/card[1]
        
        cards = new Card[5];
        cards[0] = karty[numerGracza][0];
        cards[1] = karty[numerGracza][1];
        cards[2] = karty[numerGracza][2];
        cards[3] = karty[numerGracza][3];
        cards[4] = karty[numerGracza][4];
        
        Karty = gra.getKarty();
        Karty[0][0].setText(Croupier.showFigure(cards[0].returnCard()[0]));
        Karty[0][1].setText(Croupier.showColor(cards[0].returnCard()[1]-1));       
        Karty[1][0].setText(Croupier.showFigure(cards[1].returnCard()[0]));
        Karty[1][1].setText(Croupier.showColor(cards[1].returnCard()[1]-1));  
        Karty[2][0].setText(Croupier.showFigure(cards[2].returnCard()[0]));
        Karty[2][1].setText(Croupier.showColor(cards[2].returnCard()[1]-1));  
        Karty[3][0].setText(Croupier.showFigure(cards[3].returnCard()[0]));
        Karty[3][1].setText(Croupier.showColor(cards[3].returnCard()[1]-1));  //fak maj lajf
        Karty[4][0].setText(Croupier.showFigure(cards[4].returnCard()[0]));
        Karty[4][1].setText(Croupier.showColor(cards[4].returnCard()[1]-1)); 
        /*System.out.println(cards[0].returnCard()[0]+" "+(cards[0].returnCard()[1]-1));
        System.out.println(cards[1].returnCard()[0]+" "+(cards[1].returnCard()[1]-1));
        System.out.println(cards[2].returnCard()[0]+" "+(cards[2].returnCard()[1]-1));
        System.out.println(cards[3].returnCard()[0]+" "+(cards[3].returnCard()[1]-1));
        System.out.println(cards[4].returnCard()[0]+" "+(cards[4].returnCard()[1]-1));
        System.out.println(Croupier.showFigure(cards[0].returnCard()[0])+" "+Croupier.showColor(cards[0].returnCard()[1]-1));
        System.out.println(Croupier.showFigure(cards[1].returnCard()[0])+" "+Croupier.showColor(cards[1].returnCard()[1]-1));
        System.out.println(Croupier.showFigure(cards[2].returnCard()[0])+" "+Croupier.showColor(cards[2].returnCard()[1]-1));
        System.out.println(Croupier.showFigure(cards[3].returnCard()[0])+" "+Croupier.showColor(cards[3].returnCard()[1]-1));
        System.out.println(Croupier.showFigure(cards[4].returnCard()[0])+" "+Croupier.showColor(cards[4].returnCard()[1]-1));*/
    }
    
    void numerGracza(int i){
        nrGracza = gra.getNrGracza();
        nrGracza.setText("Gracz "+Integer.toString(i+1));
    }
    
    void Stan(int stan){
        buttons = gra.getButtons();
        chckBoxArray = gra.getCheckBoxes();
        zaklad = gra.getZaklad();
        if(stan == 1){
                        //all-in i fold
            zaklad.setEnabled(false);
            buttons[0].setEnabled(false);//check
            buttons[1].setEnabled(false); // raise
            buttons[2].setEnabled(false); // bet
            buttons[3].setEnabled(false);//Call
            buttons[4].setEnabled(true);//Fold;
            buttons[5].setEnabled(false); // wymiana
            buttons[6].setEnabled(true);//Allin
            chckBoxArray[0].setEnabled(false);
            chckBoxArray[1].setEnabled(false);
            chckBoxArray[2].setEnabled(false);
            chckBoxArray[3].setEnabled(false);
            chckBoxArray[4].setEnabled(false);
        }
        if(stan == 2){
            //check/bet/fold
            zaklad.setEnabled(true);            
            buttons[0].setEnabled(true);//check
            buttons[1].setEnabled(false); // raise
            buttons[2].setEnabled(true); // bet
            buttons[3].setEnabled(false);//Call
            buttons[4].setEnabled(true);//Fold;
            buttons[5].setEnabled(false); // wymiana
            buttons[6].setEnabled(false);//Allin
            chckBoxArray[0].setEnabled(false);
            chckBoxArray[1].setEnabled(false);
            chckBoxArray[2].setEnabled(false);
            chckBoxArray[3].setEnabled(false);
            chckBoxArray[4].setEnabled(false);
        }
        if(stan == 3){
                        //call/raise/fold/all-in
            zaklad.setEnabled(true);            
            buttons[0].setEnabled(false);//check
            buttons[1].setEnabled(true); // raise
            buttons[2].setEnabled(false); // bet
            buttons[3].setEnabled(true);//Call
            buttons[4].setEnabled(true);//Fold;
            buttons[5].setEnabled(false); // wymiana
            buttons[6].setEnabled(true);//Allin
            chckBoxArray[0].setEnabled(false);
            chckBoxArray[1].setEnabled(false);
            chckBoxArray[2].setEnabled(false);
            chckBoxArray[3].setEnabled(false);
            chckBoxArray[4].setEnabled(false);
        }
        if(stan == 4){
                        //wymiana
            zaklad.setEnabled(false);            
            buttons[0].setEnabled(false);//check
            buttons[1].setEnabled(false); // raise
            buttons[2].setEnabled(false); // bet
            buttons[3].setEnabled(false);//Call
            buttons[4].setEnabled(false);//Fold;
            buttons[5].setEnabled(true); // wymiana
            buttons[6].setEnabled(false);//Allin
            chckBoxArray[0].setEnabled(true);
            chckBoxArray[1].setEnabled(true);
            chckBoxArray[2].setEnabled(true);
            chckBoxArray[3].setEnabled(true);
            chckBoxArray[4].setEnabled(true);
        }
    }
    
    void next(){
        
        start.setNext();
    }
    
    void Check() {

        start.setZagranie(1, 0);
    }
    
    void Bet(int iloscKasy) {

        start.setZagranie(2, iloscKasy);
    }
    
    void Fold() {

        start.setZagranie(3,0);
    }
    
    void Raise(int iloscKasy) {

        start.setZagranie(4, iloscKasy);
    }
    
    void Call() {
        
        start.setZagranie(5,0);
    }
    
    void all_In() {
        System.out.println("HATGAT");
        start.setZagranie(6,0);
    }
    
    void wymiana() {
        chckBoxArray = gra.getCheckBoxes();
        Komunikaty = gra.getKomunikaty();
        
        String komunikat = "";
        boolean chck = false;
        if (chckBoxArray[0].isSelected()){
            komunikat = komunikat + "1";
            chckBoxArray[0].setSelected(false);
            chck = true;
        }
        if (chckBoxArray[1].isSelected()){
            komunikat = komunikat + "2";
            chckBoxArray[1].setSelected(false);
            chck = true;            
        }
        if (chckBoxArray[2].isSelected()){
            komunikat = komunikat + "3";
            chckBoxArray[2].setSelected(false);
            chck = true;            
        }
        if (chckBoxArray[3].isSelected()){
            komunikat = komunikat + "4";
            chckBoxArray[3].setSelected(false);
            chck = true;            
        }
        if (chckBoxArray[4].isSelected()){
            komunikat = komunikat + "5";
            chckBoxArray[4].setSelected(false);
            chck = true;            
        }
        if (chck == false){
            komunikat = komunikat + "0";
        } 
        Komunikaty.setText(komunikat);
        System.out.println(komunikat);
        start.setZagranieWymiana(komunikat);
    }
    
    /*void showCards(Card[] karty) {
        
    }*/
    
    void updateCash(int numerGracza,int tokens) {
        kasaGraczy = gra.getKasaLabel();
        kasaGraczy[numerGracza].setText(Integer.toString(tokens));
    }
    
    void updateStolCash(int tokens,int stawka) {
        JLabel kasaStol = gra.getKasaStol();
        JLabel lblStawka = gra.getStawka();
        kasaStol.setText(Integer.toString(tokens));
        lblStawka.setText(Integer.toString(stawka));
    }
    
    void updateScore(int numerGracza,int score) {
        scoreGraczy = gra.getWygraneLabel();
        scoreGraczy[numerGracza].setText(Integer.toString(score));
    }
    
    void getGame(Gra gra){this.gra = gra;}
    
}